﻿<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:uc="clr-namespace:SqlMiM.Common.UserControls;assembly=SqlMiM.Common"             
    xmlns:src="clr-namespace:SqlMiM.Common.Controls;assembly=SqlMiM.Common"
    xmlns:conv="clr-namespace:SqlMiM.Common.Converters;assembly=SqlMiM.Common"
    xmlns:vsm="clr-namespace:System.Windows;assembly=WPFToolkit" 
    x:Class="SqlMiM.SqlIndexes.MissingIndexes.View"
    xmlns:local="clr-namespace:SqlMiM.SqlIndexes.MissingIndexes"
    xmlns:viewModel="clr-namespace:SqlMiM.SqlIndexes.ViewModel"
    xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"    
    xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:chartingprimitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
    Loaded="UserControl_Loaded"
    mc:Ignorable="d" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit">

    <UserControl.Resources>

        <ResourceDictionary>

            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/SqlMiM.Common;component/Resources/ControlsTheme.xaml"/>
                <ResourceDictionary Source="/SqlMiM.Common;component/Resources/DatagridTheme.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <conv:BooleanToVisibilityConverter x:Key="VisibleIfTrue" Inverted="True" Not="False"    />
            <conv:BooleanToVisibilityConverter x:Key="VisibleIfNotTrue" Inverted="True" Not="True"    />
            <conv:ReverseBooleanConverter x:Key="ReverseBooleanConverter" />

            <DataTemplate DataType="{x:Type viewModel:DatabaseView}">
                <DockPanel LastChildFill="True" Margin="0,3,0,3" Background="Transparent" >
                    <Image VerticalAlignment="Center" DockPanel.Dock="Left" Margin="5,0,0,0"  
                           Source="/SqlMim.SqlIndexes;component/Images/database.png" Width="16" />
                    <CheckBox Name="chk" VerticalAlignment="Center" IsChecked="{Binding IsChecked, Mode=TwoWay}" 
                              DockPanel.Dock="Left" Margin="5,0,0,0" />
                    <TextBlock VerticalAlignment="Center" Grid.Column="1" Text="{Binding DatabaseName}" />
                </DockPanel>


            </DataTemplate>

            <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}" x:Key="myListItemStyle">
                <Setter Property="IsSelected"  Value="{Binding IsChecked, Mode=TwoWay}" />
                <Setter Property="Foreground"  Value="{DynamicResource Foreground}" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Grid SnapsToDevicePixels="true" Margin="0">
                                <Border x:Name="Border" Margin="0" Background="{TemplateBinding Background}" 
                                    BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                                <ContentPresenter />
                            </Grid>
                            <ControlTemplate.Triggers>

                                <Trigger Property="IsSelected" Value="true">
                                    <Setter Property="Background" Value="{DynamicResource BrushMedium}" TargetName="Border"/>
                                    <Setter Property="Foreground" Value="{DynamicResource BrushOffWhite}" />
                                </Trigger>

                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Foreground" Value="{DynamicResource BrushDisabledSunkenBackground}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style TargetType="{x:Type ListBox}" x:Key="myListStyle" BasedOn="{StaticResource {x:Type ListBox}}">
                <Setter Property="ItemContainerStyle" Value="{StaticResource myListItemStyle}" />
                <Setter Property="Margin"  Value="0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBox}">
                            <Grid>
                                <Border Name="Border">
                                    <Border BorderThickness="0,1,0,0" BorderBrush="#6000" Background="#10FFFFFF" 
                                            CornerRadius="2,3,2,2"  SnapsToDevicePixels="True">
                                        <ScrollViewer Focusable="false" Background="Transparent" SnapsToDevicePixels="True">
                                            <DockPanel LastChildFill="True" SnapsToDevicePixels="True">
                                                <DockPanel DockPanel.Dock="Top" SnapsToDevicePixels="True" >
                                                    <Image Source="/SqlMim.SqlIndexes;component/Images/database_go.png" Width="16" Margin="5,0,0,0"  SnapsToDevicePixels="True" DockPanel.Dock="Left" />
                                                    <Button  MinWidth="18" MinHeight="24" Margin="2,0,0,0" Cursor="Hand" DockPanel.Dock="Left"
                                                    VerticalAlignment="Top" HorizontalAlignment="Left" Command="{Binding SelectElements}" 
                                                             CommandParameter="{x:Static local:SelectType.SelectAll}" SnapsToDevicePixels="True" >
                                                        <Button.Template>
                                                            <ControlTemplate>
                                                                <Image Source="/SqlMim.SqlIndexes;component/Images/tick.png" Width="16" Margin="0"  SnapsToDevicePixels="True"  />
                                                            </ControlTemplate>
                                                        </Button.Template>
                                                    </Button>
                                                    <Button  MinWidth="18" MinHeight="24" Margin="2,0,0,0" Cursor="Hand" DockPanel.Dock="Left"
                                                    VerticalAlignment="Top" HorizontalAlignment="Left" Command="{Binding SelectElements}" CommandParameter="{x:Static local:SelectType.UnselectAll}" SnapsToDevicePixels="True" >
                                                        <Button.Template>
                                                            <ControlTemplate>
                                                                <Image Source="/SqlMim.SqlIndexes;component/Images/cross.png" Width="16" Margin="0"  SnapsToDevicePixels="True"  />
                                                            </ControlTemplate>
                                                        </Button.Template>
                                                    </Button>
                                                    <!--<Button MinWidth="18" MinHeight="24" Margin="2,0,0,0" Cursor="Hand" DockPanel.Dock="Left"
                                                    VerticalAlignment="Top" HorizontalAlignment="Left" Command="{Binding SelectElements}" CommandParameter="{x:Static local:SelectType.ReverseSelection}" SnapsToDevicePixels="True" >
                                                        <Button.Template>
                                                            <ControlTemplate>
                                                                <Image Source="/SqlMim.SqlIndexes;component/Images/link_break.png" Width="16" Margin="0"  SnapsToDevicePixels="True"  />
                                                            </ControlTemplate>
                                                        </Button.Template>
                                                    </Button>-->
                                                </DockPanel>
                                                <StackPanel IsItemsHost="true" />
                                            </DockPanel>
                                        </ScrollViewer>
                                    </Border>
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Background" Value="{StaticResource BrushDisabledSunkenBackground}" TargetName="Border"/>
                                    <Setter Property="Foreground" Value="{StaticResource BrushDisabledForeground}"/>
                                    <Setter Property="Opacity" Value="0.75"/>
                                </Trigger>
                                <Trigger Property="IsGrouping" Value="true">
                                    <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>


        </ResourceDictionary>
    </UserControl.Resources>


    <DockPanel LastChildFill="True" >


        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <ContentControl Grid.Column="0" Margin="0,0,5,0" HorizontalAlignment="Stretch" >
                <ContentControl.Content>
                    <StackPanel Orientation="Vertical">

                        <ContentControl Style="{DynamicResource TitleContentControlStyle}">
                            <ContentControl.Content>
                                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="Databases" />
                            </ContentControl.Content>
                        </ContentControl>

                        <ListBox SelectionMode="Multiple" ItemsSource="{Binding Databases}" Style="{StaticResource myListStyle}" />

                        <Button Command="{Binding SelectPotentialIndexesCommand}" Content="Get Indexes." Margin="0,10,0,0" HorizontalAlignment="Right" Width="120"  />

                    </StackPanel>
                </ContentControl.Content>
            </ContentControl>
            
            <GridSplitter HorizontalAlignment="Right" Grid.Column="0" Width="8" Margin="0,10,0,0" Background="Transparent" />

            <my:DataGrid  Grid.Column="1" Margin="0,0,0,0" CanUserReorderColumns="True" CanUserResizeRows="False" CanUserAddRows="False" 
                          Background="Transparent"
                CanUserDeleteRows="True" CanUserResizeColumns="True" CanUserSortColumns="True"  HeadersVisibility="Column" SelectionUnit="FullRow"
                IsReadOnly="True" SelectionMode="Extended"  Name="grdIndexes" Style="{StaticResource SqlMiM.DataGrid}"
                AutoGenerateColumns="False" ItemsSource="{Binding PotentialIndexesView.View}"  >
                <my:DataGrid.ContextMenu>
                    <ContextMenu x:Name="datagridContextMenu" >
                        <MenuItem Header="Create this Index" Command="ApplicationCommands.New"  />
                    </ContextMenu>

                </my:DataGrid.ContextMenu>

                <my:DataGrid.Columns>

                    <my:DataGridTemplateColumn Width="250" Header="Object" SortMemberPath="PotentialIndex.DatabaseSchemaTable">
                        <my:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Image Height="20" Width="20" Margin="1" Stretch="Uniform" 
                                                           Source="/SqlMiM.SqlIndexes;component/Images/database_gear.png" />
                                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                        <TextBlock Margin="2,0,6,0" Text="{Binding PotentialIndex.DatabaseSchemaTable}" />
                                    </StackPanel>
                                </StackPanel>
                            </DataTemplate>
                        </my:DataGridTemplateColumn.CellTemplate>
                    </my:DataGridTemplateColumn>


                    <my:DataGridTextColumn Header="IndexedColumns" Binding="{Binding Path=PotentialIndex.IndexedColumns}"  
                                           SortMemberPath="PotentialIndex.IndexedColumns"  Width="200" />

                    <my:DataGridTextColumn Header="IncludedColumns" Binding="{Binding Path=PotentialIndex.IncludedColumns}"  
                                           SortMemberPath="PotentialIndex.IncludedColumns"  Width="200" />

                    <my:DataGridTemplateColumn Header="Potential % Gain" Width="120" SortMemberPath="PotentialIndex.PotentialPctGainUserImpact">
                        <my:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Border Margin="4,1,1,2" BorderThickness="1"  Width="100" Name="border1"
                                                VerticalAlignment="Top" HorizontalAlignment="Left"
                                                BorderBrush="{StaticResource BrushGreen}"
                                                Background="{DynamicResource BrushMedium}">
                                        <Grid>
                                            <Rectangle Name="rectangle1" StrokeThickness="0" 
                                                           Width="{Binding PotentialIndex.PotentialPctGainUserImpact}" HorizontalAlignment="Left" Margin="0"
                                                           Fill="{StaticResource GradientBrushGreen}"  />
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock  FontWeight="Bold" VerticalAlignment="Center" 
													            HorizontalAlignment="Left" Margin="2,0,0,0" 
													            Text="{Binding PotentialIndex.PotentialPctGainUserImpact, StringFormat={}{0:0.00}}" />
                                                <TextBlock  FontWeight="Bold" VerticalAlignment="Center" 
													            HorizontalAlignment="Left" Margin="2,0,0,0" 
													            Text="%" />
                                            </StackPanel>
                                        </Grid>
                                    </Border>


                                </StackPanel>
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding PotentialIndex.PotentialPctGainUserImpact}" Value="1">
                                        <Setter TargetName="rectangle1" Property="Fill" 
                                                    Value="{StaticResource GradientBrushGreen}" />
                                        <Setter TargetName="border1" Property="BorderBrush" 
                                                    Value="{StaticResource BrushGreen}" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding PotentialIndex.PotentialPctGainUserImpact}" Value="2">
                                        <Setter TargetName="rectangle1" Property="Fill" 
                                                    Value="{StaticResource GradientBrushOrange}" />
                                        <Setter TargetName="border1" Property="BorderBrush" 
                                                    Value="{StaticResource BrushOrange}" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding PotentialIndex.PotentialPctGainUserImpact}" Value="3">
                                        <Setter TargetName="rectangle1" Property="Fill" 
                                                    Value="{StaticResource GradientBrushRed}" />
                                        <Setter TargetName="border1" Property="BorderBrush" 
                                                    Value="{StaticResource BrushRed}" />
                                    </DataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </my:DataGridTemplateColumn.CellTemplate>
                    </my:DataGridTemplateColumn>

                    <my:DataGridTextColumn Header="Potential impact" Binding="{Binding Path=PotentialIndex.PotentialImpact}"  
                                           SortMemberPath="PotentialIndex.PotentialImpact"  Width="110" />


                    <my:DataGridTextColumn Header="Potential seeks"  Binding="{Binding Path=PotentialIndex.PotentialUserSeeks}"  
                                           SortMemberPath="PotentialIndex.PotentialUserSeeks" Width="100" />

                    <my:DataGridTextColumn Header="Potential last seek" Binding="{Binding PotentialIndex.PotentialLastUserSeek, StringFormat={}{0:d/M/yyyy HH:mm:ss}}"  
                                           SortMemberPath="PotentialIndex.PotentialLastUserSeek" Width="130" />

                    <my:DataGridTextColumn Header="Potential scans"  Binding="{Binding Path=PotentialIndex.PotentialUserScans}"  
                                           SortMemberPath="PotentialIndex.PotentialUserScans" Width="100" >
                    </my:DataGridTextColumn>

                    <my:DataGridTextColumn Header="Potential last scan"  Binding="{Binding Path=PotentialIndex.PotentialLastUserScan, StringFormat={}{0:d/M/yyyy HH:mm:ss}}" 
                                       SortMemberPath="PotentialIndex.PotentialLastUserScan" Width="130"  >
                    </my:DataGridTextColumn>
                    

                </my:DataGrid.Columns>

                <!--<Expander IsExpanded="True" Style="{StaticResource ExpanderForListView}">-->
                <my:DataGrid.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Margin" Value="5,5,5,5" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander IsExpanded="True" >
                                                <Expander.Header>
                                                    <Grid>
                                                        <StackPanel Margin="0,2" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center">
                                                            <TextBlock FontWeight="Bold" Margin="0,0,0,0" Foreground="{DynamicResource Foreground}" TextWrapping="WrapWithOverflow">
                                                                        <TextBlock.Text>
                                                                          <Binding Path="Name" StringFormat="Database : {0}" />
                                                                        </TextBlock.Text>
                                                            </TextBlock>
                                                            <TextBlock FontWeight="Bold" Margin="5,0,0,0" Foreground="{DynamicResource Foreground}">
                                                                        <TextBlock.Text>
                                                                          <Binding Path="Items.Count" StringFormat="({0})"  />
                                                                        </TextBlock.Text>
                                                            </TextBlock>
                                                        </StackPanel>
                                                    </Grid>
                                                </Expander.Header>
                                                <Expander.Content>
                                                    <ItemsPresenter  HorizontalAlignment="Left" VerticalAlignment="Center" />
                                                </Expander.Content>
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>

                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <my:DataGridRowsPresenter HorizontalAlignment="Left" VerticalAlignment="Top"  />
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Padding="3"/>
                                </StackPanel>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>

                    </GroupStyle>
                </my:DataGrid.GroupStyle>

            </my:DataGrid>

        </Grid>

    </DockPanel>

</UserControl>
